﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.Common;
using SzCourt.Logic.NBL.DBEntities;
using SzCourt.Logic.NBL;
using NBearLite;
using KMFrame.Core;

namespace SzCourt.Logic.BLL
{
    public class RefereeDoc : SzCourtLogic
    {
        private ConditionCollection GetRefereeDoc(RfDocCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.pRefereeDoc.RefereeDocID, 
                SZCourtColumns.pRefereeDoc.CaseName,
                SZCourtColumns.pRefereeDoc.CauseOfAction,
                SZCourtColumns.pRefereeDoc.CaseLevel,  
                SZCourtColumns.pRefereeDoc.DocLookedUp, 
                SZCourtColumns.pRefereeDoc.CaseClass, 
                SZCourtColumns.pRefereeDoc.AddDate,
                SZCourtColumns.pRefereeDoc.DocPublishDate
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = WhereClip.All;
            if (condition.CaseClass != null && condition.CaseClass.Length > 0)
                where = where && SZCourtColumns.pRefereeDoc.CaseClass.Like(condition.CaseClass + "%");
            if (condition.CaseName != null && condition.CaseName.Length > 0)
                where = where && SZCourtColumns.pRefereeDoc.CaseName.Contains(condition.CaseName);
            if (condition.CaseContent != null && condition.CaseContent.Length > 0)
                where = where && SZCourtColumns.pRefereeDoc.CaseContent.Contains(condition.CaseContent);
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(SZCourtColumns.pRefereeDoc, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.pRefereeDoc.RefereeDocID)
                .OrderBy(SZCourtColumns.pRefereeDoc.DocPublishDate.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 根据条件取文书列表，仅返回列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<RefereeDocResult> GetRefereeDocList(RfDocCondition condition)
        {
            return GetRefereeDoc(condition).Select.ToList<RefereeDocResult>();
        }



        /// <summary>
        /// 根据条件取文书列表，可翻页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetRefereeDocPage(RfDocCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetRefereeDoc(condition).Select.ToList<RefereeDocResult>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pRefereeDoc, SZCourtColumns.pRefereeDoc.RefereeDocID, GetRefereeDoc(condition).Where);
            }

            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public IEnumerable<pRefereeDoc> GetRefereeDocTop(string caseClass)
        {
            return NBLData.Select(SZCourtColumns.pRefereeDoc)
                .Where(SZCourtColumns.pRefereeDoc.CaseClass.Like(caseClass + '%'))
                .SetSelectRange(20, 0, SZCourtColumns.pRefereeDoc.RefereeDocID)
                .OrderBy(SZCourtColumns.pRefereeDoc.DocPublishDate.Desc)
                .ToList<pRefereeDoc>();
        }

        /// <summary>
        /// 取某文书详细资料
        /// </summary>
        /// <param name="DocID"></param>
        /// <returns></returns>
        public pRefereeDoc GetRefereeDocInfo(int DocID)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.pRefereeDoc.RefereeDocID, 
                SZCourtColumns.pRefereeDoc.CaseName,
                SZCourtColumns.pRefereeDoc.CauseOfAction, 
                SZCourtColumns.pRefereeDoc.DocLookedUp,
                SZCourtColumns.pRefereeDoc.AddDate,
                SZCourtColumns.pRefereeDoc.CaseContent,
            };
            return NBLData
                .Select(SZCourtColumns.pRefereeDoc)
                .Where(SZCourtColumns.pRefereeDoc.RefereeDocID == DocID)
                .ToList<pRefereeDoc>().FirstOrDefault();
        }

        //获取裁判文书评论
        public List<pRefereeDocComment> GetpCommentList(int DocID)
        {
            return NBLData
                .Select(SZCourtColumns.pRefereeDocComment)
                .Where(SZCourtColumns.pRefereeDocComment.RefereeDocID == DocID && SZCourtColumns.pRefereeDocComment.IsAudit == 1)
                .ToList<pRefereeDocComment>();
        }

        /// <summary>
        /// 新增评论
        /// </summary>
        /// <param name="DocID"></param>
        /// <returns></returns>
        public bool AddComment(pRefereeDocComment comment)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(SZCourtColumns.pRefereeDocComment)
                .AddColumn(SZCourtColumns.pRefereeDocComment.RefereeDocID, comment.RefereeDocID)
                .AddColumn(SZCourtColumns.pRefereeDocComment.Conent, comment.Conent)
                .AddColumn(SZCourtColumns.pRefereeDocComment.AddDateTime, DateTime.Now)
                .AddColumn(SZCourtColumns.pRefereeDocComment.AddUserID, comment.AddUserID)
                .AddColumn(SZCourtColumns.pRefereeDocComment.AddUserName, comment.AddUserName)
                .AddColumn(SZCourtColumns.pRefereeDocComment.AddIP, comment.AddIP)
                .AddColumn(SZCourtColumns.pRefereeDocComment.IsAudit, 0);
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        //获取未审核得裁判文书评论
        public PageResult GetpCommentList(PageParam param)
        {            
            PageResult result = new PageResult();

            result.ResultList =NBLData
                .Select(SZCourtColumns.pRefereeDocComment)               
                .SetSelectRange(param.PageSize, param.PageSize * (param.PageIndex - 1), SZCourtColumns.pRefereeDocComment.CommentID)
                .OrderBy(SZCourtColumns.pRefereeDocComment.CommentID.Desc)
                .ToList<pRefereeDocComment>();

            int total = -1;
            if (param.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pRefereeDocComment, SZCourtColumns.pRefereeDocComment.CommentID, WhereClip.All);
            }

            result.PageIndex = param.PageIndex;
            result.PageSize = param.PageSize;
            result.TotalCount = total;
            return result;
        }

        /// <summary>
        /// 取某某一评论
        /// </summary>
        /// <param name="DocID"></param>
        /// <returns></returns>
        public pRefereeDocComment GetCommentInfo(int commentID)
        {           
            return NBLData
                .Select(SZCourtColumns.pRefereeDocComment)
                .Where(SZCourtColumns.pRefereeDocComment.CommentID == commentID)
                .ToList<pRefereeDocComment>().FirstOrDefault();
        }
            
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public bool AuditComment(int commentID)
        {
             if (commentID <= 0)
                return false;
            UpdateSqlSection update = NBLData.Update(SZCourtColumns.pRefereeDocComment)
                .AddColumn(SZCourtColumns.pRefereeDocComment.IsAudit,1)               
                .Where(SZCourtColumns.pRefereeDocComment.CommentID == commentID);

            int ri = update.Execute();
            if (ri <= 0)
                return false;
            return true;        
        }
    }

    public class RfDocCondition : PageParam
    {
        public string CaseClass { get; set; }
        public string CaseName { get; set; }
        public string CaseContent { get; set; }
    }

    public class RefereeDocResult
    {
        public int RefereeDocID { get; set; }
        public string CaseName { get; set; }
        public string CauseOfAction { get; set; }
        public string CaseLevel { get; set; }
        public string DocLookedUp { get; set; }
        public string CaseClass { get; set; }
        public DateTime AddDate { get; set; }
    }
}
